package Employee; 

import SalaryComponent.EmpSalDetailsBean;
import SalaryComponent.EmpSalaryBean;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Vector;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Mukesh
 */
public class AddEmployeeSalDetails extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        try {
            int empid = 0;
            if (request.getParameter("empid").length() != 0) {
                empid = Integer.parseInt(request.getParameter("empid"));
            }
            SalaryComponent.EmpSalaryBean empBean = new EmpSalaryBean();
            if (request.getParameter("tote").length() != 0) {
                empBean.setTotalEarning(Float.parseFloat(request.getParameter("tote")));
            }
            if (request.getParameter("tota").length() != 0) {
                empBean.setTotalSal(Float.parseFloat(request.getParameter("tota")));
            }
            if (request.getParameter("totd").length() != 0) {
                empBean.setTotalDeduction(Float.parseFloat(request.getParameter("totd")));
            }
            empBean.setUserId(empid);
            int empSal = 0;
            empBean.setEmpSalId(SalaryComponent.Query.chkEmpSal(empid));
            if (empBean.getEmpSalId() != 0) {
                empSal = SalaryComponent.Query.editEmpSal(empBean);

            } else {
                empSal = SalaryComponent.Query.addEmpSal(empBean);
            }
//-----------------------------------------------------------------
            if (empSal != 0) {
                Vector<Object> list = new Vector<Object>();
                String[] token = request.getParameter("noc").split("-");
                for (int k = 0; k < token.length; k++) {
                    if (token[k].length() != 0) {
                        String[] compId = token[k].split("/");
                        EmpSalDetailsBean esdb = new EmpSalDetailsBean();
                        esdb.setEmpSalId(empSal);
                        esdb.setSalCompoId(Integer.parseInt(compId[0]));
                        out.println(compId[0]);
                        if (request.getParameterValues(compId[0] + "-c") != null) {
                            esdb.setAmount(Float.parseFloat(request.getParameter(compId[0])));
                        } else {
                            continue;
                        }
                        list.add(esdb);
                    }
                }
                SalaryComponent.Query.deleteEmpSal(empSal);
                int salDetail = SalaryComponent.Query.addEmpSalDetails(list);

                if (salDetail != 0) {
                    session.setAttribute("msg", "Salary Fixation done Successfully.");
                    session.setAttribute("status", "success");
                    response.sendRedirect("/RIT/Services/Payroll/SalaryFixationSearch.jsp");
                } else {
                    session.setAttribute("msg", "Salary Fixation not done.");
                    session.setAttribute("status", "Fail");
                    response.sendRedirect("/RIT/Services/Payroll/SalaryFixationSearch.jsp");
                }
            } else {
                session.setAttribute("msg", "Salary Fixation not done.");
                session.setAttribute("status", "Fail");
                response.sendRedirect("/RIT/Services/Payroll/SalaryFixationSearch.jsp");
            }
        } catch (Exception e) {
            session.setAttribute("msg", "Salary Fixation not done.(e)");
            session.setAttribute("status", "Fail");
            response.sendRedirect("/RIT/Services/Payroll/SalaryFixationSearch.jsp");
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
